//
//  WaitTakeFooterView.m
//  BaishidaiKnight
//
//  Created by mac book on 2019/1/28.
//  Copyright © 2019年 sy. All rights reserved.
//

#import "WaitTakeFooterView.h"
#import "MQGradientProgressView.h"

@interface WaitTakeFooterView ()

@property(nonatomic,strong) UIView *addressView;
@property(nonatomic,strong) UIButton * iconBtn;
@property(nonatomic,strong) UILabel* sendAddressLabel;
@property(nonatomic,strong) TTTAttributedLabel* reGeocodeLabel;

@property(nonatomic,strong) UIView *proView;
@property(nonatomic,strong) TTTAttributedLabel* orderTimeLabel;
@property(nonatomic,strong) TTTAttributedLabel* orderLabel;
@property(nonatomic,strong) TTTAttributedLabel* arriveTimeLabel;
@property(nonatomic,strong) TTTAttributedLabel* arriveLabel;
@property(nonatomic,strong) MQGradientProgressView *progressView;

@property(nonatomic,strong) UIButton * stateBtn;
@property(nonatomic,strong) UIView * bottomLineView;

@end


@implementation WaitTakeFooterView


-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){

        [self createSubviews];
    }
    return self;
}

#pragma mark - createSubviews
-(void)createSubviews{
    
    [self addSubview:self.addressView];
    [self.addressView addSubview:self.iconBtn];
    [self.addressView addSubview:self.sendAddressLabel];
    [self.addressView addSubview:self.reGeocodeLabel];
    
    [self addSubview:self.stateBtn];
    [self addSubview:self.proView];
    [self.proView addSubview:self.arriveLabel];
    [self.proView addSubview:self.orderLabel];
    [self.proView addSubview:self.orderTimeLabel];
    [self.proView addSubview:self.arriveTimeLabel];
    [self.proView addSubview:self.progressView];
    
    [self addSubview:self.bottomLineView];

    
    
    [self.addressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(60);
    }];
    
    [self.iconBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.addressView);
        make.size.mas_equalTo(CGSizeMake(25, 25));
        make.left.equalTo(self.addressView).offset(10);
    }];
    
    [self.sendAddressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.addressView);
        make.left.equalTo(self.iconBtn.mas_right).offset(10);
        make.right.equalTo(self.addressView).offset(-10);
        make.height.mas_equalTo(30);
        
    }];
    
    [self.reGeocodeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.sendAddressLabel.mas_bottom);
        make.left.equalTo(self.sendAddressLabel);
        make.right.equalTo(self.sendAddressLabel);
        make.bottom.equalTo(self.addressView);
        
    }];
    
    
    [self.stateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.proView);
        make.right.equalTo(self).offset(-20);
        make.size.mas_equalTo(CGSizeMake(100, 35));
    }];
    
    
    
    float label_h = 40;
    float label_w = 80;
    float progress_h = 5;
    
    [self.proView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.addressView.mas_bottom);
        make.left.equalTo(self).offset(5);
        make.height.mas_equalTo(100);
        make.right.equalTo(self.stateBtn.mas_left).offset(-10);
        
    }];
    
    [self.orderTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.proView);
        make.left.equalTo(self.proView);
        make.size.mas_equalTo(CGSizeMake(label_w, label_h));
    }];
    
    
    [self.arriveTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.orderTimeLabel);
        make.right.equalTo(self.proView);
        make.size.equalTo(self.orderTimeLabel);
    }];
    
    
    
    [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.proView);
        make.height.mas_equalTo(progress_h);
        make.left.equalTo(self.proView).offset(label_w/2);
        make.right.equalTo(self.proView).offset(-label_w/2);
        
    }];
    
    [self.orderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.proView);
        make.left.equalTo(self.orderTimeLabel);
        make.size.equalTo(self.orderTimeLabel);
    }];
    
    [self.arriveLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.orderLabel);
        make.right.equalTo(self.arriveTimeLabel);
        make.size.equalTo(self.arriveTimeLabel);
    }];
    
    [self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(10);
        make.left.right.equalTo(self);
        make.bottom.equalTo(self);
    }];
    
}

-(void)setTaskModel:(TaskModel *)taskModel{
    
    _taskModel = taskModel;
    
    self.sendAddressLabel.text = taskModel.sendaddress;
    self.reGeocodeLabel.text = taskModel.address_road;
    
    self.arriveTimeLabel.text = taskModel.order_end_time;
    self.orderTimeLabel.text = taskModel.order_start_time;
    self.progressView.progress = [taskModel.rate floatValue];
    
    
    NSString *now = [CommonUntils currentDateStr];
    NSString *end = [CommonUntils getDateStringWithTimeStr:taskModel.order_end_time_stamp];
    
    NSLog(@"%@",now);
    NSLog(@"end = %@",end);
    NSLog(@"order_end_time_stamp = %@",taskModel.order_end_time_stamp);
    //        [kCountDownManager start];
    
    int result = [CommonUntils compareDate:now withDate:end];
    if (result == 1) {//送达时间大于当前时间
        
        NSString *time = [CommonUntils dateFromNowDate:taskModel.order_end_time_stamp];
        taskModel.timeCount = [time intValue];
        taskModel.isTimeOut = NO;
        // 手动刷新数据
        // 监听通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countDownListNotification) name:OYCountDownNotification object:nil];
        
    }else if (result == -1){//送达时间小于当前时间  说明已超时
        
        NSString *time = [CommonUntils dateFromOutDate:taskModel.order_end_time_stamp];
        taskModel.timeCount = [time intValue];
        taskModel.isTimeOut = YES;
        
        
        int hour = [time intValue]/3600;
        if (hour >= 10) {
            [[NSNotificationCenter defaultCenter] removeObserver:self name:OYCountDownNotification object:nil];
            
            //                [kCountDownManager invalidate];
            [self.stateBtn setTitle:@"已超时" forState:UIControlStateNormal];
            
        }else{
            // 手动刷新数据
            // 监听通知
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countDownListNotification) name:OYCountDownNotification object:nil];
            
            
        }
        
    }else{
        
        
    }
    
    
    
    
    
}


#pragma mark - 懒加载
-(UIButton *)iconBtn{
    if(!_iconBtn){
        _iconBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_iconBtn setImage:[UIImage imageNamed:@"icon_sendstatus_light"] forState:UIControlStateNormal];
        _iconBtn.userInteractionEnabled = NO;
        _iconBtn.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _iconBtn;
}

-(UILabel *)sendAddressLabel{
    if(!_sendAddressLabel){
        _sendAddressLabel = [[UILabel alloc]init];
        _sendAddressLabel.font = [UIFont systemFontOfSize:14.f];
        _sendAddressLabel.lineBreakMode = NSLineBreakByCharWrapping;
        _sendAddressLabel.numberOfLines = 0;
        _sendAddressLabel.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _sendAddressLabel;
}

-(TTTAttributedLabel *)reGeocodeLabel{
    if(!_reGeocodeLabel){
        _reGeocodeLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
        _reGeocodeLabel.textColor = [UIColor grayColor];
        _reGeocodeLabel.font = [UIFont systemFontOfSize:12.f];
        _reGeocodeLabel.lineBreakMode = NSLineBreakByCharWrapping;
        _reGeocodeLabel.numberOfLines = 0;
        _reGeocodeLabel.translatesAutoresizingMaskIntoConstraints = NO;
        _reGeocodeLabel.verticalAlignment=TTTAttributedLabelVerticalAlignmentTop;
        
    }
    return _reGeocodeLabel;
}
-(UIView *)addressView{
    if(!_addressView){
        _addressView = [[UIView alloc]init];
    }
    return _addressView;
}

-(UIView *)proView{
    if(!_proView){
        _proView = [[UIView alloc]init];
    }
    return _proView;
}

-(MQGradientProgressView *)progressView{
    if(!_progressView){
        float w = SCREEN_WIDTH - 35 - 100 - 80;
        _progressView = [[MQGradientProgressView alloc] initWithFrame:CGRectMake(0, 0,  w, 5)];
        //    progressView.colorArr = @[(id)MQRGBColor(59, 221, 255).CGColor,(id)MQRGBColor(34, 126, 239).CGColor];
        _progressView.colorArr = @[(id)[UIColor blueColor]];
    }
    return _progressView;
}



-(TTTAttributedLabel *)orderTimeLabel{
    if(!_orderTimeLabel){
        _orderTimeLabel = [[TTTAttributedLabel alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        _orderTimeLabel.font = [UIFont systemFontOfSize:17.f];
        _orderTimeLabel.textAlignment = NSTextAlignmentCenter;
        _orderTimeLabel.textColor  = kMyColor(23, 155, 193);
        _orderTimeLabel.verticalAlignment=TTTAttributedLabelVerticalAlignmentBottom;
        
    }
    return _orderTimeLabel;
}

-(TTTAttributedLabel *)orderLabel{
    if(!_orderLabel){
        _orderLabel = [[TTTAttributedLabel alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        _orderLabel.font = [UIFont systemFontOfSize:13.f];
        _orderLabel.textAlignment = NSTextAlignmentCenter;
        _orderLabel.textColor  = [UIColor grayColor];
        _orderLabel.text = @"下单时间";
        _orderLabel.verticalAlignment=TTTAttributedLabelVerticalAlignmentTop;
        
    }
    return _orderLabel;
}

-(TTTAttributedLabel *)arriveTimeLabel{
    if(!_arriveTimeLabel){
        _arriveTimeLabel = [[TTTAttributedLabel alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        _arriveTimeLabel.font = [UIFont systemFontOfSize:17.f];
        _arriveTimeLabel.textAlignment = NSTextAlignmentCenter;
        _arriveTimeLabel.textColor  =kMyColor(23, 155, 193);
        _arriveTimeLabel.verticalAlignment=TTTAttributedLabelVerticalAlignmentBottom;
        
    }
    return _arriveTimeLabel;
}

-(TTTAttributedLabel *)arriveLabel{
    if(!_arriveLabel){
        _arriveLabel = [[TTTAttributedLabel alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        _arriveLabel.font = [UIFont systemFontOfSize:13.f];
        _arriveLabel.textAlignment = NSTextAlignmentCenter;
        _arriveLabel.textColor  =  [UIColor grayColor];
        _arriveLabel.text = @"预计到达时间";
        _arriveLabel.verticalAlignment=TTTAttributedLabelVerticalAlignmentTop;
        
    }
    return _arriveLabel;
}

-(UIButton *)stateBtn{
    if(!_stateBtn){
        _stateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_stateBtn setBackgroundColor:kMyColor(244, 155, 51)];
        _stateBtn.layer.cornerRadius = 35/2;
        _stateBtn.layer.masksToBounds = YES;
        _stateBtn.titleLabel.font = [UIFont systemFontOfSize:14.f];
    }
    return _stateBtn;
}
-(UIView *)bottomLineView{
    if(!_bottomLineView){
        _bottomLineView = [[UIView alloc]init];
        _bottomLineView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    }
    return _bottomLineView;
}


#pragma mark - 倒计时通知回调
- (void)countDownListNotification {
    /// 判断是否需要倒计时 -- 可能有的cell不需要倒计时,根据真实需求来进行判断
    if (0) {
        return;
    }
    /// 计算倒计时
    TaskModel *model = self.taskModel;
    NSInteger timeInterval;
    if (model.countDownSource) {
        timeInterval = [kCountDownManager timeIntervalWithIdentifier:model.countDownSource];
    }else {
        timeInterval = kCountDownManager.timeInterval;
    }
    
    
    int countDown = 0;
    if (model.isTimeOut) {
        
        countDown =  model.timeCount + timeInterval;
    }else{
        
        countDown =  model.timeCount - timeInterval;
        
    }
    
    
    /// 重新赋值
    //右侧等待支付
    NSString *restTime = [NSString stringWithFormat:@"%02d:%02d:%02d",(countDown/3600),(countDown/60)%60, countDown%60];
    NSString *time = [NSString stringWithFormat:@"剩余 %@",restTime];
    NSString *outtime = [NSString stringWithFormat:@"已超时 %@",restTime];
    
    if (model.isTimeOut) {
        
        [self.stateBtn setTitle:outtime forState:UIControlStateNormal];
        
    }else{
        
        [self.stateBtn setTitle:time forState:UIControlStateNormal];
        /// 当倒计时到了进行回调
        if (countDown <= 0) {
            
            // 回调给控制器
            if (self.countDownListZero) {
                self.countDownListZero(model);
            }
            
            return;
        }
    }
    
    
    
}


- (void)dealloc {
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}






/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
